package com.hongxun.budget.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.utils.MyImageLoader;
import com.hongxun.budget.utils.T;
import com.hongxun.entity.LoginInfo;
import com.hongxun.interfaces.NovaCallback;
import com.previewlibrary.ZoomMediaLoader;

import butterknife.ButterKnife;
import kr.co.namee.permissiongen.PermissionGen;

/**
 * Created by admin on 2018/7/17.
 */

public abstract class BaseActivity extends AppCompatActivity implements NovaCallback {


    public Context context;
    public Activity activity;
    public boolean isPause;
    public NoticeDialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(getLayoutId());
        super.onCreate(savedInstanceState);
        context = this;
        activity = this;
        ButterKnife.bind(this);
        ZoomMediaLoader.getInstance().init(new MyImageLoader());//设置图片加载器，显示图片
        dialog = new NoticeDialog();
        initView(savedInstanceState);
    }

    public abstract int getLayoutId();

    public abstract void initView(Bundle savedInstanceState);


    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
        isPause = false;
    }

    public void initData() {
    }

    /**
     * 消息提示
     *
     * @param msg
     */
    public void showMsg(String msg) {
        if (dialog == null) dialog = new NoticeDialog();
        if(dialog.isVisible()) dialog.dismiss();
        dialog.setTitle("提示").setMsg(msg).setCancelButton(null, null).setConfirmButton("确定", null).show(getFragmentManager(), "");
    }

    @Override
    public void err(int i, int status, String err) {
        if ((status == 500 && err.equals("token失效，请重新登录"))
                || (status == 401 && err.equals("invalid token")) || (status == 500 && err.equals("token不能为空"))) {
            LoginInfo.remove(context);
            startActivity(new Intent(context, LoginActivity.class));
            T.showLong(context, err);
            finish();
        } else {
            showMsg(err);
        }
    }

    @Override
    public void success(int i, JSONObject obj) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    public String getText(TextView view) {
        return view.getText().toString().trim();
    }

}